<template>
	<div>
      <!-- <div class="container"> -->
		<div class="handle-box">
			<!-- <label class="label" >专业方向：</label> -->
			<el-input class="handle-input" v-model="headerData.testPaperName" placeholder="考试名称"/>
			<!-- <label class="label" >状态：</label> -->
			<el-select
			  v-model="headerData.state" 
				filterable
				allow-create
                placeholder="状态" 
			   >
				<el-option
					v-for="item in options.statuses"
					:key="item.value"
					:label="item.label"
					:value="item.value"
					/>
			</el-select>
			<el-select
			  v-model="headerData.classification" 
				filterable
				allow-create
                placeholder="分类"  
			   >
				<el-option
					v-for="item in options.classifications"
					:key="item.value"
					:label="item.label"
					:value="item.value"
					/>
			</el-select>
            <el-date-picker
                    v-model="headerData.time"
                    type="datetimerange"
                    range-separator="到"
                    start-placeholder="开始时间"
                    end-placeholder="结束时间"
                    style="width: 300px;"
                    />
				<el-button color="#1c84c6" class="handle-btn"  @click="handleSearch">查询</el-button>
            </div>
			<el-table ref="multipleTableRef" 
				:header-cell-style="{color:'#2A62BC'}"
			 	:data="tableData"   
			 	style="width: 100%;" 
                size="small"
                border
				@selection-change="handleSelectionChange">
                <el-table-column   type="selection" width="35" />
				<el-table-column  prop="testPaperName" label="考试|考试"  align="center">
					<template #default="scope">
                        <p>{{scope.row.testSubjects  }}</p>
					<p class="p13"> <i class="iconfont icon-list-alt3 "></i>{{ scope.row.testPaperName }}</p>	
					</template>
				</el-table-column>
				<el-table-column  prop="classification" label="分类"  align="center">
                </el-table-column>
                <el-table-column label="状态" align="center">
                    <template #default="scope">
                        <el-switch
                                v-model="scope.row.state"
                                :active-value="'启用'"
                                :inactive-value="'禁用'"
                                active-color="#64bd63"
                                inactive-color="#c0c0c0"
                                
                                @change="changeSwitch(scope.row.state)"/>
					</template>
                </el-table-column>
                <el-table-column   label="考试时间"  align="center">
                    <template #default="scope">
                        <p>{{scope.row.startTime  }}</p>
					<p class="p13">{{ scope.row.endTime }}</p>	
					</template>
                </el-table-column>
				<el-table-column  label="总分" align="center">
					<template #default="scope">
						<a class="test-success" v-if="scope.row.totalScore>=80">{{ scope.row.totalScore}}</a>
						<a class="test-waring" v-else-if="scope.row.totalScore>=60">{{ scope.row.totalScore}}</a>
						<a class="test-danger" v-else>{{ scope.row.totalScore}}</a>
					</template>
				</el-table-column>
                <el-table-column  prop="citations" label="考试人次"  align="center">
					<template #default="scope">
					<p>
						<el-tooltip content="点我看详情" placement="bottom" effect="light">
						<a class="test-success"  @click="lookRecord(scope.row)">{{ scope.row.citations }}</a>
					</el-tooltip>
						次
					</p>
					</template>
				</el-table-column>
                <el-table-column label="最后修改时间" align="center">
					<template #default="scope">
					<p>{{  scope.row.modifier }}</p>	
					<p style="color:#676A6C">{{ scope.row.lastModifiedTime }}</p>
					</template>
				</el-table-column>
				<el-table-column label="操作" width="220" align="center" >
					<template #default="scope">
						<el-button-group class="right-btn">
							<el-tooltip content="考试记录" placement="bottom" effect="light">
						    <el-button   @click="lookRecord(scope.row)" >
                                <i class="iconfont icon-eye1 "></i></el-button>
							</el-tooltip>
							<el-tooltip content="修改" placement="bottom" effect="light">
						    <el-button  style="color: #1c64c6; font-family: 900;" :icon="Edit" @click="open(scope.$index,scope.row)" >
                              </el-button>
							</el-tooltip>
							<el-tooltip content="删除" placement="bottom" effect="light">
				            <el-button  style="color: #Ed5565; font-family: 900;" :icon="Delete" @click="handleDelete(scope.$index)">
                            </el-button>
						</el-tooltip>
						</el-button-group>
					</template>
				</el-table-column>
			</el-table>
			<!-- 分页 -->
			<div class="pagination">
				<el-pagination
				:current-page="query.pageIndex"
					:page-size="query.pageSize"
					:page-sizes="[5, 10, 15, 20]"
					size="small"
					layout="total, sizes, prev, pager, next, jumper"
					:total="pageTotal"
					@size-change="handleSizeChange"
					@current-change="handlePageChange"/>
			</div>
	
		</div>
	<!-- </div> -->
</template>

<script setup >
import { ref, reactive,toRefs } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import { Delete, Edit, Search, Plus } from '@element-plus/icons-vue';
import { getPaperK,getClazzes,getStus,getPaperInfo} from '@/api/exam' ;
const isFull = ref(false)
//组件数据传递
const  props= defineProps(['btnState'])
const emit  = defineEmits(['sendRowData','update:btnState','sendRecord'])
/** --------创建考试 和考试管理按钮-----------  */

/*--------头部数据--------*/
const headerData  = reactive({
	testPaperName:'',
	state:'',
	classification:'',
	type:'',
    time:''

})

const options = reactive({
statuses:[
	{
		value: '状态',
		label: '状态',
 	 },
  {
    value: '正常',
    label: '正常',
  },
  {
    value: '禁用',
    label: '禁用',
  },
  ],
  classifications:[
	{
		value: '统考',
		label: '统考',
 	 },
  {
    value: '个人能力考试',
    label: '个人能力考试',
  },
  {
    value: '政策',
    label: '政策',
  },
  ],
  types:[
  {
    value: '普通试卷',
    label: '普通试卷',
  },
  {
    value: '随机试卷',
    label: '随机试卷',
  },
  ]
}
)
	
//分页字体
const small = ref(true)
// const size = ref<'default' | 'large' | 'small'>('default')


//分页数据
const query = reactive({
	pageIndex: 1,
	pageSize: 5,
});
//

//表格数据
const tableData = ref();
//总页数
const pageTotal = ref(0);
const multipleTableRef = ref()
const state = reactive({
	multipleSelection:[]
})

// 获取表格数据  分页是数据截取  后台结合一起后  直接返回分页的后的数据
const getData = () => {
    getPaperK({pageCurrent:query.pageIndex,pageSize:query.pageSize}).then(res=>{
        console.log(res,"---res---")
        // tableData.value = res.data.
		let start  = (query.pageIndex-1)*query.pageSize;//开始位置
        let end =query.pageIndex*query.pageSize  ;//结束位置 数组的截取的时候 [start,end) suo所以不用-1
        tableData.value = res.data.slice(start,end);
        // tableData.value = res.data;
        pageTotal.value = res.data.length
    })
	
};
getData();
//多选框权限
const handleSelectionChange = (val) => {
  state.multipleSelection = val
}
// 分页导航
const handleSizeChange = (val) => {
  // console.log(`${val} items per page`)
  query.pageSize = val;
  getData()
}
const handlePageChange = (val) => {
	query.pageIndex = val;
	console.log(val,"==")
	console.log(query.pageSize)
	getData()
};


//是够启用调用后台接口
const  changeSwitch=(status)=>{
    //调用后台修改接口
    console.log(status,'>>>status');
}

// 删除单个操作
const handleDelete = (index) => {
	// 二次确认删除
	ElMessageBox.confirm('确定要删除吗？', '提示', {
		type: 'warning'
	})
	.then(() => {
		ElMessage.success('删除成功');
		//to 传递id 到后台 
		tableData.value.splice(index, 1);
		// 删除成功后
		// getData()
	})
	.catch(() => {});
};

// 查询操作  to... 需要自己实现与后台交互
const handleSearch = () => {
	query.pageIndex = 1;
	getData();
};

// 点击修改
const open  =  async (index,row)=>{//index是时间源对象
	console.log(row,'点击修改')
	let  dataList  = {
		id:'',
		testPaperName: '',//考试名称
		classification:'',//考试分类
		startUsing:'正常',//考试状态
		setTestPaper:'',//设置考试
		passMark:'',//几个分数
		trySorting :'',//试题排序
		tryShow:'',//考试展示
		optionRandom:'',//选项随机
		canTestTimes:'',//可靠次数
		testInstructions:'',//考试说明
		startTime:'',//开始时间
		endTime:'',//结束时间
		testTime:'',//考试时长
		releaseTime:'',//公布成绩
		submitLimit:'',//参加限制
		toLimit:'',//交卷限制
		announcedAnswer:'',//答案公布
		checkDetails:'',//查看详情
		classes:[],
		stus:[]
	}
	
	//获取被选中的班级
   let classes  =await  getClazzes({}).then(res=>{
		/* let clazzes = new Array() ;
		res.data.forEach(item=>{
			if(item.check){
				clazzes.push(item)
			}
		})
		return clazzes */
		return  res.data.filter(item=>{
			if(item.check){
				return item
			}
		})

	 })

	//获取被选中的学生
	let students = await getStus({}).then(res=>{
		/* let stus = new Array() ;
		res.data.forEach(item=>{
			if(item.check){
				stus.push(item)
			}
		}) */
		 return  res.data.filter(item=>{
			if(item.check){
				return item
			}
		})
	 })
	 //试卷信息 
	let  paper =  await getPaperInfo({}).then(res=>{
		return  res.data
	 })
	 console.log(paper,'papersss')
    dataList.id =row.id
	dataList.testPaperName=row.testPaperName//考试名称
	dataList.classification=row.classification//考试分类
	dataList.startUsing=row.state//考试状态
	dataList.setTestPaper=row.testPaperName//设置考试
	dataList.trySorting =row.trySorting//试题排序
	dataList.tryShow=row.paperShow//考试展示
	dataList.optionRandom=row.trySorting,//选项随机
	dataList.startTime=row.startTime//开始时间
	dataList.endTime=row.endTime//结束时间
	dataList.classes=classes
	dataList.stus =students
	dataList.passMark=paper[0].passMark//及格分数
	dataList.canTestTimes=paper[0].canTestTimes//可靠次数
	dataList.testInstructions=paper[0].testInstructions,//考试说明
	dataList.testTime=paper[0].testTime//考试时长
	dataList.releaseTime=paper[0].releaseTime//公布成绩
	dataList.submitLimit=paper[0].submitLimit//参加限制
	dataList.toLimit=paper[0].toLimit//交卷限制
	dataList.announcedAnswer=paper[0].announcedAnswer//答案公布
	dataList.checkDetails=paper[0].checkDetails//查看详情
	console.log(dataList,'点击修改添加班级和学生之后')
	emit('sendRowData',dataList)
	emit('update:btnState',3)
	
}

/***-------考试记录---------*/
const lookRecord = (row)=>{
	// emit("sendRowData",row)
	emit("sendRecord",{flag:'r',data:row})

}

</script >
<style lang="less" scoped>
.handle-box {
	margin-bottom: 20px;   
	.handle-btn{
		margin-left: 10px;
	}
}

.el-select {
	width: 150px;
	margin-right: 10px;  
}

.handle-input {
	width: 200px;
    margin-right: 10px;
}

.mr10 {
	margin-right: 10px;
}


.top-btn{
	float: right;
	.el-button{
		margin-left: 5px;
	}
}
.right-btn{
 	justify-content:center;
	.el-button{
		height: 22px;
		// width: 60px;
		margin-right: 10px;
		padding: 1px 5px;
	}
}
.test-success{
	font-size: 20px;
	color: #1c84c6;
	font-weight: 700;
}

.test-waring{
	font-size: 20px;
	color: #F8AC59;
	font-weight: 700;
}
.test-danger{
	font-size: 20px;
	color: #ED5565;
	font-weight: 700;
}

.my-header {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  border-bottom: 1px  solid #f0f0f0;
}

.form-item /deep/.el-form-item__label{
	color:#676A6C;
	font-weight: bold;
	font-size: 13px;
}
.form-item .el-input{
	width: 85%;
}
.panel-heading{
	color: #333;
	background-color: #f5f5f5;
	border-color: #ddd;
	width: 85%;
}
:deep(.my-content) {
  background: var(--el-color-danger-light-9); 
  color:red;
}

.p13{
    color: #888888;
}
.el-radio-group{
    float:right;
    margin-right: 15px;
}
</style>